<?php


namespace App\Libraries\CustomsBills;


use Illuminate\Support\Collection;
use Sabre\Xml\Service;
use \Sabre\Xml\Deserializer;
use Sabre\Xml\Reader;

class Analysis
{
    /**
     * @var array
     * @author luffyzhao@vip.126.com
     */
    protected $elementMap;


    /**
     * Analysis constructor.
     * @param string $input
     * @throws \Sabre\Xml\ParseException
     * @author luffyzhao@vip.126.com
     */
    public function __construct(string $input)
    {
        $this->elementMap = $this->setElementMap($input);
    }

    /**
     * @return Collection
     * @author luffyzhao@vip.126.com
     */
    public function getOrderReturn(){
        return Collection::make($this->elementMap)->where('name', '=', '{http://www.chinaport.gov.cn/ceb}OrderReturn');
    }

    /**
     * @param string $input
     * @return array
     * @throws \Sabre\Xml\ParseException
     * @author luffyzhao@vip.126.com
     */
    protected function setElementMap(string $input): array
    {
        $service = new Service();
        $service->elementMap = [
            '{http://www.chinaport.gov.cn/ceb}OrderReturn' => function (Reader $reader) {
                return Deserializer\keyValue($reader, 'http://www.chinaport.gov.cn/ceb');
            }
        ];
        return $service->parse($input, 'http://www.chinaport.gov.cn/ceb');
    }
}